package com.luochen.cdpt.job;

import com.luochen.cdpt.eneity.FeePolicy;
import com.luochen.cdpt.eneity.QFeePolicy;
import com.luochen.cdpt.repository.IFeePolicyRepository;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Auther: star
 * @Date: 2019/1/30 11:02
 * @Description:定时任务
 */
@Component
@Slf4j
public class ScheduleJobs {


    @Autowired
    IFeePolicyRepository feePolicyRepository;

    /**
     *定时更新费用政策失效状态
     *每天凌晨1点执行一次
     */
    @Scheduled(cron = "0 0 1 * * ?")
    @Transactional
    public void updateFeePolicyStatusJob() {
        QFeePolicy qFeePolicy = QFeePolicy.feePolicy;
        Predicate predicate = qFeePolicy.status.eq("1").
                and(qFeePolicy.expiredDate.before(new Date()));
        List<FeePolicy> list = (List<FeePolicy>) feePolicyRepository.findAll(predicate);
        if (null != list && list.size() > 0) {
            list.forEach(feePolicy -> {
                feePolicy.setStatus("0");//失效
            });
            feePolicyRepository.saveAll(list);
        }
    }

}
